package com.xcms.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {

    //Java 虚拟机原理、内存划分
    //new HelloServlet()执行两件事情
    //1.找内存中是否有HelloServlet这个Class，如果没有从磁盘加载
    //2.根据字节码创建对象
    public HelloServlet(){

        System.out.println("HelloServlet create");
    }

    @Override
    //Servlet 初始化的时候执行一次
    //作用：初始化一些Servlet资源
    public void init() throws ServletException {
        System.out.println("init");
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();//向网页输出打印
        out.println("Hello Servlet");
        out.close();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();//向网页输出打印
        out.println("Hello Servlet");
        out.close();
        System.out.println("do get");
        System.out.println(this.getServletConfig().getInitParameter("name"));
    }

    @Override
    public void destroy() {//Servlet 销毁的时候执行一次
        System.out.println("destroy");
    }

}
